package com.hz.ruoyistorage.controller;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.ruoyistorage.domain.LgColdStorage;
import com.hz.ruoyistorage.domain.LgSubCold;
import com.hz.ruoyistorage.domain.mypojo.LgSubColdSome;
import com.hz.ruoyistorage.mapper.LgSubColdMapper;
import com.hz.ruoyistorage.service.LgColdStorageService;
import com.hz.ruoyistorage.service.LgSubColdService;
import com.hz.ruoyistorage.service.LgSubColdSomeService;
import com.hz.ruoyistorage.service.LgUnitService;
import com.hz.ruoyistorage.utils.ResultJSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author gj
 * @Date 2025/3/6 9:54
 * @注释
 */
@RestController
@RequestMapping("/smallCold")
public class LgSubColdController {
    @Autowired
    private LgSubColdService lgSubColdService;
    @Autowired
    private LgSubColdSomeService lgSubColdSomeService;
    @Autowired
    private LgSubColdMapper lgSubColdMapper;
    @Autowired
    private LgColdStorageService lgColdStorageService;
    //获得表格数据
//    @RequiresPermissions("storage:smallCold:getAll")
    @PostMapping("/getAll")
    public ResultJSON getAll(@RequestBody Map<String, Object> query){
        //获取当前页
        Integer pageNum = Integer.parseInt(query.get("pageNum").toString());
        //获取每页条数
        Integer pageSize = Integer.parseInt(query.get("pageSize").toString());
        query.put("createBy2",String.valueOf(BaseDomain.getUserId()));
        query.put("firstNum",(pageNum-1)*pageSize);
        query.put("secondNum",pageSize);
//        if(query.get("parentId")!=null){
//            String parentId = query.get("parentId").toString();
//            System.out.println("======================================"+parentId);
//        }
        //算钱
        List<LgSubColdSome> lgSubColdSomes = lgSubColdSomeService.updateDailyPrice(lgSubColdSomeService.getAll(query));
        return ResultJSON.success(lgSubColdSomes);
    }
    //获得数据条数
//    @RequiresPermissions("storage:smallCold:getAll")
    @PostMapping("/getTotal")
    public ResultJSON getTotal(@RequestBody Map<String, Object> query){
        query.put("createBy2",String.valueOf(BaseDomain.getUserId()));
        return ResultJSON.success(lgSubColdSomeService.getTotal(query));
    }
    //获得冷库类型
//    @RequiresPermissions("storage:smallCold:getAll")
    @PostMapping("/getColdType")
    public ResultJSON getColdType(){
        return ResultJSON.success(lgSubColdService.getColdType());
    }

    //新增小冷库
//    @RequiresPermissions("storage:smallCold:add")
    @PostMapping("/addSmallCold")
    public ResultJSON addSmallCold(@RequestBody LgSubCold lgSubCold){
        lgSubCold.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        LgColdStorage lgColdStorage = lgColdStorageService.getById(lgSubCold.getParentId());
        Double remainingVolume = lgColdStorage.getRemainingVolume();
        System.out.println("剩余容积："+remainingVolume);
        //算钱
        //获得容积
        Double firstNum = lgSubCold.getCapacity();
        System.out.println("容积："+firstNum);
        if (remainingVolume<firstNum){
            return ResultJSON.success(-1,"容积不足！");
        }
        //获得单价
        Integer aaa = lgSubCold.getColdId();
        Double secondNum = Double.valueOf(lgSubColdService.getPriceMeter(aaa));
        lgSubCold.setDailyPrice(firstNum*secondNum);
        return ResultJSON.success(lgSubColdService.save(lgSubCold));
    }

    //根据id查找小冷库
//    @RequiresPermissions("storage:smallCold:getAll")
    @GetMapping(value = "/{subId}")
    public ResultJSON getSmallColdById(@PathVariable  Integer subId){
        return ResultJSON.success(lgSubColdService.getById(subId));
    }
    //修改小冷库
//    @RequiresPermissions("storage:smallCold:update")
    @PostMapping("/updateSmallCold")
    public ResultJSON updateSmallCold(@RequestBody LgSubCold lgSubCold){
        lgSubColdService.updateDailyPrice(lgSubCold);
        return ResultJSON.success(lgSubColdService.updateById(lgSubCold));
    }
    //获得id,name,parentId
//    @RequiresPermissions("storage:smallCold:getAll")
    @GetMapping("/getIdAndName")
    public ResultJSON getIdAndName(){
        return ResultJSON.success(lgSubColdService.getIdAndName());
    }

    //导出
    @RequiresPermissions("storage:smallCold:export")
    @Log(title = "小冷库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgSubCold lgSubCold)
    {

        List<LgSubCold> list = lgSubColdService.list(null);
        ExcelUtil<LgSubCold> util = new ExcelUtil<LgSubCold>(LgSubCold.class);
        util.exportExcel(response, list, "小冷库数据");
    }
//    @RequiresPermissions("storage:smallCold:delete")
    @PostMapping(value = "/{subIds}")
    public ResultJSON deleteSmallCold(@PathVariable Long[] subIds){
        for (Long subId : subIds){

            lgSubColdMapper.deleteById(subId);
        }
        return ResultJSON.success();
    }
}
